/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.valueObject.methods;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.repository.GspDbDataType;
import com.inspur.edp.cef.designtime.api.IFieldCollection;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.repository.valueObject.ValueObjectAssemblerGenCtx;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import lombok.var;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;

public abstract class ValueObjInitColumnMethodGenerator extends ClassMethodGenerator {
  private IGspCommonDataType dataType;
  private ValueObjectAssemblerGenCtx parentContext;

  protected ValueObjInitColumnMethodGenerator(
          ValueObjectAssemblerGenCtx parentContext, IGspCommonDataType dataType) {
    this.dataType = dataType;
    this.parentContext = parentContext;
  }

  @Override
  protected Block buildMethodBody() {
    ArrayList<Statement> statements = new ArrayList<Statement>();
    Block block = ast.newBlock();
    IFieldCollection elementList = getElements();
    for (IGspCommonField element : elementList) {
      if (element.getIsVirtual()) {
        continue;
      }
      ExpressionStatement statement = buildAddColumnStatement(element);
      statements.add(statement);
    }
    block.statements().addAll(statements);
    return block;
  }

  protected abstract IFieldCollection getElements();

  private ExpressionStatement buildAddColumnStatement(IGspCommonField element) {
    ArrayList<Expression> arguments = buildArgumentList(element, null);

    SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
    invocation.setName(ast.newSimpleName("addColumn"));
    invocation.arguments().addAll(arguments);
    return ast.newExpressionStatement(invocation);

  }


  private ArrayList<Expression> buildArgumentList(
      IGspCommonField element, IGspCommonElement belongElement) {

    ArrayList<Expression> argumentList =
        new ArrayList<Expression>();

    StringLiteral paramLabelId = ast.newStringLiteral();
    paramLabelId.setLiteralValue(element.getLabelID());
    argumentList.add(paramLabelId);

    ClassInstanceCreation paramProcesser = ast.newClassInstanceCreation();
    TypeInfo processer = getProcessType(element.getLabelID());
    paramProcesser.setType(processer.getType(ast, parentContext.getRepoGenCtx()));
    argumentList.add(paramProcesser);

    BooleanLiteral paramMulti = ast.newBooleanLiteral(element.getIsMultiLanguage());
    argumentList.add(paramMulti);

    BooleanLiteral paramUdt = ast.newBooleanLiteral(element.getIsUdt());
    argumentList.add(paramUdt);

    BooleanLiteral paramAsso = ast.newBooleanLiteral(element.getObjectType() == GspElementObjectType.Association);
    argumentList.add(paramAsso);

    BooleanLiteral paramEnum = ast.newBooleanLiteral(element.getObjectType() == GspElementObjectType.Enum);
    argumentList.add(paramEnum);

    StringLiteral paramBelongElement = ast.newStringLiteral();
    paramBelongElement.setLiteralValue(belongElement == null ? "" : belongElement.getLabelID());
    argumentList.add(paramBelongElement);

    GspDbDataType dbType = getDbType(element);
    QualifiedName paramType = ast.newQualifiedName(ast.newName(GspDbDataType.class.getTypeName()), ast.newSimpleName(dbType.toString()));
    argumentList.add(paramType);

    return argumentList;
  }

  private GspDbDataType getDbType(IGspCommonField element) {
    switch (element.getMDataType()) {
      case Boolean:
        return GspDbDataType.Char;
      case Date:
      case DateTime:
        return GspDbDataType.DateTime;
      case Decimal:
        return GspDbDataType.Decimal;
      case String:
        if (element.getIsMultiLanguage()) {
          return GspDbDataType.NVarChar;
        } else {
          return GspDbDataType.VarChar;
        }
      case Integer:
        return GspDbDataType.Int;
      case Binary:
        return GspDbDataType.Blob;
      case Text:
        if (element.getIsMultiLanguage()) {
          return GspDbDataType.NClob;
        } else {
          return GspDbDataType.Clob;
        }
      default:
        return GspDbDataType.VarChar;
    }
  }

  protected abstract TypeInfo getProcessType(String columnName);

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new ArrayList<Modifier.ModifierKeyword>(
        Arrays.asList(
            new Modifier.ModifierKeyword[] {
                    Modifier.ModifierKeyword.PUBLIC_KEYWORD
            }));
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return "initColumnInfo";
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return new ArrayList<ParameterInfo>();
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("void");
  }
}
